//
//  ThirdLogin.m
//  project
//
//  Created by 圆圆陈 on 2018/4/6.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "ThirdLogin.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
#import "WXApi.h"
@implementation ThirdLogin
+ (instancetype) sharedInstance{
    
    static ThirdLogin* instance = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!instance) {
            instance = [[ThirdLogin alloc]init];
        }
    });
    
    return instance;
}
-(instancetype)init{
    self = [super init];
    if (self) {
        
    }
    return self;
}
-(void)registerActivePlatforms{
    [ShareSDK registerActivePlatforms:@[
                                       @(SSDKPlatformTypeSinaWeibo),
                                       @(SSDKPlatformTypeMail),
                                       @(SSDKPlatformTypeSMS),
                                       @(SSDKPlatformTypeCopy),
                                       @(SSDKPlatformTypeWechat),
                                       @(SSDKPlatformTypeQQ),
                                       @(SSDKPlatformTypeRenren),
                                       @(SSDKPlatformTypeFacebook),
                                       @(SSDKPlatformTypeTwitter),
                                       @(SSDKPlatformTypeGooglePlus),
                                       @(SSDKPlatformTypeFacebookMessenger)
                                       ]
onImport:^(SSDKPlatformType platformType)
    {
        switch (platformType)
        {
            case SSDKPlatformTypeWechat:
                [ShareSDKConnector connectWeChat:[WXApi class]];
                break;
            case SSDKPlatformTypeQQ:
                [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
                break;
            default:
                break;
        }
    }
onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo)
    {
        switch (platformType)
        {
            case SSDKPlatformTypeWechat:
                [appInfo SSDKSetupWeChatByAppId:WECHATLOGINID
                                      appSecret:WECHATLOGINKEY];
                break;
            case SSDKPlatformTypeQQ:
                [appInfo SSDKSetupQQByAppId:QQLOGINID
                                     appKey:QQLOGINKEY
                                   authType:SSDKAuthTypeBoth];
                break;
            case SSDKPlatformTypeTwitter:
                [appInfo SSDKSetupTwitterByConsumerKey:TwitterByConsumerKey
                                        consumerSecret:TwitterConsumerSecret
                                           redirectUri:TwitterRedirectUri];
                break;
            default:
                break;
        }
    }];
}
-(void)QQLoginWithSuccess:(NSObjectSimpleBlock)success fail:(NSObjectSimpleBlock)fail{
    [ShareSDK getUserInfo:SSDKPlatformTypeQQ onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
        [ShareSDK cancelAuthorize:SSDKPlatformTypeQQ];
        if (error) {
            SHOWMBProgressHUD(nil, error.localizedDescription, nil, NO, 2);
            return ;
        }
        if (user.uid!=nil) {
            SHOWMBProgressHUDIndeterminate(nil, nil, @"登录中");
            [SN_Net loginOnWithOpenId:user.uid withOpenType:@"qq" Success:^(id object) {
                success(object);
            } fail:^(id object) {
                
            }];
        }
    }];
}
-(void)WechatLoginWithSuccess:(NSObjectSimpleBlock)success fail:(NSObjectSimpleBlock)fail{
    
    [ShareSDK getUserInfo:SSDKPlatformTypeWechat onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
        [ShareSDK cancelAuthorize:SSDKPlatformTypeWechat];
        if (error) {
            SHOWMBProgressHUD(nil, error.localizedDescription, nil, NO, 2);
            return ;
        }
        if (user.uid!=nil) {
            SHOWMBProgressHUDIndeterminate(nil, nil, @"登录中");
            [SN_Net loginOnWithOpenId:user.uid withOpenType:@"weixin" Success:^(id object) {
                success(object);
            } fail:^(id object) {
                
            }];
        }
    }];
}
-(void)TwitterLoginWithSuccess:(NSObjectSimpleBlock)success fail:(NSObjectSimpleBlock)fail{
    [ShareSDK getUserInfo:SSDKPlatformTypeTwitter onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
        [ShareSDK cancelAuthorize:SSDKPlatformTypeTwitter];
        if (error) {
            SHOWMBProgressHUD(nil, error.localizedDescription, nil, NO, 2);
            return ;
        }
        if (user.uid!=nil) {
            SHOWMBProgressHUDIndeterminate(nil, nil, @"登录中");
            [SN_Net loginOnWithOpenId:user.uid withOpenType:@"twitter" Success:^(id object) {
                success(object);
            } fail:^(id object) {
                
            }];
        }
    }];
}
@end
